#!/usr/bin/env python3

import sys
import os


def parse_conf(_f, _func):
    _ret = dict()
    with open(_f, 'r') as _fd:
        for _l in _fd.readlines():
            _ll = _l.strip()

            # skip null lines
            if not _ll:
                continue

            # skip comment lines
            if _ll.find("#") == 0:
                continue

            eidx =  _ll.find(")")
            if eidx == -1:
                eidx =  _ll.find(",")
                if eidx == -1:
                  continue

            substring = _ll[ : eidx + 1].strip()
            func_idx = substring.find(_func)
            if func_idx == -1:
                continue
            prev_idx = substring.find(":")
            func_string = substring[ prev_idx + 1:func_idx].strip()
            if func_string.find("=") != -1:
              continue
            eidx =  _ll.find("(")
            if eidx == -1:
              continue

            eidx_tmp = substring.find(",")
            if eidx_tmp != -1:
              substring = _ll[eidx : eidx_tmp].strip()
              if substring.find(" ") == -1:
                continue
              substring = _ll[ : eidx_tmp].strip()
            elif eidx_tmp == -1:
              eidx_tmp =  substring.find("void")
              if eidx_tmp == -1:
                  eidx_tmp =  substring.find(")")
                  if eidx_tmp == -1:
                    continue
                  substring = substring[ : eidx_tmp].strip()
                  if substring.find(" ") == -1:
                    continue
                  substring = _ll[eidx : eidx_tmp].strip()
                  if substring.find(" ") == -1:
                    continue
            substring = _ll[ : eidx_tmp].strip()

            eidx =  substring.find(".c")
            if eidx == -1:
                continue

            substring = substring[ : eidx].strip()

            if substring.find("tools") > 0:
                continue

            # get kv
            eidx = substring.rfind("/")
            key = substring[0:eidx+1].strip()
            count = key.count("/")
            if count < 4:
                continue
            print("" + key);
            return 1

    return 0
def parse_conf_header(_f, _func, symbol):
    _ret = dict()
    with open(_f, 'r') as _fd:
        for _l in _fd.readlines():
            _ll = _l.strip()

            # skip null lines
            if not _ll:
                continue

            # skip comment lines
            if _ll.find("#") == 0:
                continue

            eidx =  _ll.find(")")
            if eidx == -1:
                eidx =  _ll.find(",")
                if eidx == -1:
                  continue

            substring = _ll[ : eidx + 1].strip()
            func_idx = substring.find(_func)
            if func_idx == -1:
                continue
            prev_idx = substring.find(":")
            func_string = substring[ prev_idx + 1:func_idx].strip()
            if func_string.find("=") != -1:
              continue
            eidx =  _ll.find("(")
            if eidx == -1:
              continue

            eidx =  _ll.find(symbol)
            if eidx == -1:
              continue

            eidx_tmp =  substring.find(")")
            if eidx_tmp == -1:
              continue
            substring = _ll[ : eidx_tmp].strip()

            if substring.find("tools") > 0:
                continue

            eidx =  substring.find(".c")
            if eidx == -1:
                eidx =  substring.find(".h")
            if eidx == -1:
              continue

            # get kv
            eidx = substring.rfind("/")
            key = substring[0:eidx+1].strip()
            count = key.count("/")
            if count < 4:
                continue
            print("" + key);
            return 1

    return 0
if __name__ == "__main__":
    _f1 = sys.argv[1];
    _func = sys.argv[2];

    _ret = parse_conf(_f1, _func);
    if _ret == 0:
      _ret = parse_conf_header(_f1, _func, "#define");
    if _ret == 0:
      parse_conf_header(_f1, _func, "SYSCALL_DEFINE");
